package com.corner.presenter.imp;

import com.corner.model.cornerservice.domain.Attention;
import com.corner.model.cornerservice.domain.Location;
import com.corner.model.cornerservice.domain.User;
import com.corner.model.cornerservice.helper.AttentionHelper;
import com.corner.model.cornerservice.helper.LocationHelper;
import com.corner.presenter.IAttentionItemPresenter;
import com.corner.presenter.base.BasePresenter;
import com.corner.ui.adapter.AttentionAdapter;
import com.corner.ui.view.IAttentionItemView;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by Administrator on 2015/7/23，11.15.
 * 描述：
 */
public class AttentionItemPresenter extends BasePresenter<IAttentionItemView> implements IAttentionItemPresenter {
    @Override
    public void requestAvater(final Location location, final AttentionAdapter.Holder holder) {
        LocationHelper.getLocationUser(location).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<User>() {
            @Override
            public void call(User user) {
                if (view != null) {
                    location.user = user;
                    view.setUser(location, holder);
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
            }
        });
    }

    @Override
    public void requestContentNum(final Location location, final AttentionAdapter.Holder holder) {
        LocationHelper.getContentsNum(location).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
            @Override
            public void call(Long s) {
                if (view != null) {
                    location.contentNum = s.intValue();
                    view.setContentsNum(location, holder);
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
            }
        });
    }

    @Override
    public void deleteAttention(final Attention attention) {
        view.showLoading(true);
        AttentionHelper.deleteAttention(attention).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Object>() {
            @Override
            public void call(Object o) {
                view.deleteSucess(attention);
                view.showLoading(false);
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                view.showError(throwable.getMessage());
                view.showLoading(false);
            }
        });
    }
}
